/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ticketmaster.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.RowSet;
import ticketmaster.dataobject.Venue;

/**
 *
 * @author Varadha
 */
public class VenueDAO extends BaseEntityDAO {

    public VenueDAO ()
    {
        // initialization
    }

    /**
     * Insert venue into database
     * @param venue the new venue entity to be inserted
     * @return newly created venue number or a 0 on error
     */
    public int insertVenue(Venue venue)
    {
        return save(venue);
    }

    //Get location for reports 
    public RowSet getLocation()
    {
        String sql = "select Distinct(location) from venue";
        RowSet rs = query(sql);
        return rs;
    }
    /**
     * Delete venue that matches the supplied criteria
     * @param venue the supplied criteria
     * @return true on success, false on failure
     */
    public boolean deleteVenue(Venue venue)
    {
        return delete(venue);
    }

    /**
     * Update the venue using data from the venue Data Object
     * @param venue the venue Data Object to be updated
     * @return true on success, false on failure or error
     */
    public boolean updateVenue(Venue venue)
    {
        if (venue.getVenueID() == 0)
        {
            return false;
        }

        if (update(venue))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Get the venue that matches the supplied criteria
     * @param venue the supplied criteria
     * @return a Data Object if found, or null on error or if not found
     */
    public Venue findVenue(Venue venue)
    {
        String sql = null;

        if(venue.getVenueID()!=0)
            sql = buildQuerySQL(venue);
        else
            return null;

        RowSet crs = query(sql);
        try
        {
            crs.next();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(VenueDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (Venue) this.getObject(crs, venue);
    }

    /**
     * Get all venues that match the supplied criteria
     * @param venue supplied criteria
     * @return a RowSet
     */
    public RowSet getVenuesRS(Venue venue)
    {
        String sql = buildQuerySQL(venue);
        return query(sql);
    }

    /**
     * Get all venues that match the supplied criteria
     * @param venue supplied criteria
     * @return a collection of Venue Data Objects
     */
    public Collection<Venue> getVenuesDO(Venue venue)
    {
        String sql = buildQuerySQL(venue);

        RowSet crs = query(sql);
        Collection<Venue> venues = new ArrayList<Venue>();
        try
        {
            while (crs.next())
            {
                Venue venueDO = (Venue) this.getObject(crs, venue);
                Venue venueItem = new Venue();
                venueItem.setName(venueDO.getName());
                venueItem.setLocation(venueDO.getLocation());
                venueItem.setType(venueDO.getType());
                venueItem.setVenueID(venueDO.getVenueID());
                venues.add(venueItem);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(VenueDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return venues;
    }

}
